﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PoJun.Tools.Log4Net
{
    /// <summary>
    /// Log4本地日志记录工具
    /// </summary>
    public class Log4Helper
    {
        #region 初始化

        private static ILog log = null;

        private static string _configUrl = string.Empty;
        // 定义一个标识确保线程同步
        private static readonly object locker = new object();

        /// <summary>
        /// 默认构造函数
        /// </summary>
        private Log4Helper()
        {
            System.IO.FileInfo config = new System.IO.FileInfo(_configUrl);
            log4net.Config.XmlConfigurator.ConfigureAndWatch(config);
            log = log4net.LogManager.GetLogger("Common");
        }

        private static Log4Helper _instance;

        /// <summary>
        /// 获取实例
        /// </summary>
        public static Log4Helper GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new Log4Helper();
                        }
                    }
                }

                return _instance;
            }
        }

        /// <summary>
        /// 初始化日志工具类
        /// </summary>
        /// <param name="configUrl">配置文件地址</param>
        public static void Init(string configUrl = "config\\log4net.config")
        {
            if (configUrl == "config\\log4net.config")
            {
                _configUrl = System.AppDomain.CurrentDomain.BaseDirectory + configUrl;
            }
        }

        #endregion

        #region 错误信息

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="msg"></param>
        public void Error(string msg)
        {
            log.Error(msg);
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="obs">The obs.</param>
        public void Error(string msg, params object[] obs)
        {
            log.ErrorFormat(msg, obs);
        }

        /// <summary>
        /// 程序报错信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ex"></param>
        public void Error_Sys(string msg, Exception ex)
        {
            if (ex != null)
            {
                msg += "\r\n" + ex.StackTrace;
                if (ex.InnerException != null)
                {
                    msg += "\r\n内部错误：" + ex.InnerException.Message + "\r\n" + ex.InnerException.StackTrace;
                }
            }
            log.ErrorFormat(msg);
        }

        #endregion

        #region 调试信息

        /// <summary>
        /// 调试信息
        /// </summary>
        /// <param name="msg"></param>
        public void Debug(string msg)
        {
            log.Debug(msg);
        }

        /// <summary>
        /// 调试信息
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="obs">The obs.</param>
        public void Debug(string msg, params object[] obs)
        {
            log.DebugFormat(msg, obs);
        }

        #endregion

        #region 普通信息

        /// <summary>
        /// 普通信息
        /// </summary>
        /// <param name="msg"></param>
        public void Info(string msg)
        {
            log.Info(msg);
        }

        /// <summary>
        /// 普通信息
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="obs">The obs.</param>
        public void Info(string msg, params object[] obs)
        {
            log.InfoFormat(msg, obs);
        }

        #endregion

        #region 警告信息

        /// <summary>
        /// 警告信息
        /// </summary>
        /// <param name="msg"></param>
        public void Warn(string msg)
        {
            log.Warn(msg);
        }

        /// <summary>
        /// 警告信息
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="obs">The obs.</param>
        public void Warn(string msg, params object[] obs)
        {
            log.WarnFormat(msg, obs);
        }

        #endregion

        #region 灾难性信息

        /// <summary>
        /// 灾难性信息
        /// </summary>
        /// <param name="msg"></param>
        public void Fatal(string msg)
        {
            log.Fatal(msg);
        }

        /// <summary>
        /// 灾难性信息
        /// </summary>
        /// <param name="msg">The MSG.</param>
        /// <param name="obs">The obs.</param>
        public void Fatal(string msg, params object[] obs)
        {
            log.FatalFormat(msg, obs);
        }

        #endregion
    }
}
